package com.root.dreameraadmin.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.root.dreameraadmin.R;
import com.root.dreameraadmin.utils.HttpConnectionUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.root.dreameraadmin.view.ToolBarView.startIntroAnimation;

public class PointAddActivity extends AppCompatActivity {

    @Bind(R.id.add_point_toolbar)
    Toolbar toolbar;
    @Bind(R.id.toolbar_text)
    TextView toolbar_textView;
    @Bind(R.id.inputName)
    EditText point_name;
    @Bind(R.id.id_bmapView)
    MapView mMapView; //百度地图控件
    @Bind(R.id.ib_map)
    Button mapBtn;

    private static final String TAG = "PointAddActivity";
    private BaiduMap bdMap;  //百度地图对象
    private BMapManager manager;
    HashMap<String,Object> paramMap;
    BitmapDescriptor bitmap;
    private LatLng latLng = new LatLng(0.0,0.0);
    private ProgressDialog proDia;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_point_add);
        ButterKnife.bind(this);

        mapBtn.setVisibility(View.GONE);
        setSupportActionBar(toolbar);
        startIntroAnimation(toolbar, toolbar_textView,"添加新文物");

        bdMap = mMapView.getMap();
        LatLng latLng = new LatLng(41.802273,123.417315);//将地图移至沈阳市
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
        bdMap.animateMapStatus(u);

        manager = new BMapManager();
        manager.init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        mMapView.onResume();
        bdMap.setOnMapClickListener(mapClickListener);
    }


    @OnClick(R.id.ib_back)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.cancel_btn)
    public void cancelBtn() {
        clickBack();
    }

    OnMapClickListener mapClickListener = new OnMapClickListener(){
        @Override
        public void onMapClick(LatLng platLng) {
            latLng = platLng;
            ShowPoint(latLng.latitude, latLng.longitude, true);
        }

        @Override
        public boolean onMapPoiClick(MapPoi mapPoi) {
            return false;
        }
    };

    public void ShowPoint(Double latitude, Double longitude, boolean flag) {
        bdMap.clear();
        LatLng point = new LatLng(latitude,longitude);// 定义marker坐标点

        if(flag) {
            bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_marker);
        }else {
            bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_marker_red);
        }

        OverlayOptions options = new MarkerOptions() //构建markerOption，用于在地图上添加marker
                .position(point)// 设置marker的位置
                .icon(bitmap)// 设置marker的图标
                .zIndex(9)// 設置marker的所在層級
                .draggable(true);// 设置手势拖拽
        bdMap.addOverlay(options); // 在地图上添加marker，并显示

        OverlayOptions textOption = new TextOptions() //构建文字Option对象，用于在地图上添加文字
                .bgColor(0xAAFFFF00)
                .rotate(-30)
                .position(point);
        bdMap.addOverlay(textOption); //在地图上添加该文字对象并显示
    }

    @OnClick(R.id.submit_btn)
    public void submitClick() {
        paramMap = new HashMap<String, Object>();
        if (point_name.getText().toString().equals("")) {
            Toast.makeText(PointAddActivity.this, "the point name can not be null!", Toast.LENGTH_SHORT).show();
        } else {
            paramMap.put("name", point_name.getText().toString());
            paramMap.put("longitude", latLng.longitude);
            paramMap.put("latitude",  latLng.latitude);

            new AlertDialog.Builder(PointAddActivity.this)
                .setMessage("要添加这个点吗?")//设置显示的内容
                .setPositiveButton("添加", dialogPositiveListener)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//响应事件
                        dialog.dismiss();
                    }
                }).show();
        }
    }

    DialogInterface.OnClickListener dialogPositiveListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
            new addPointTask().execute();
        }
    };


    public class addPointTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            proDia = new ProgressDialog(PointAddActivity.this);
            proDia.setMessage("上传数据中...");
            proDia.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                HttpConnectionUtil.doPost(HttpConnectionUtil.PLACEURL, paramMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void i) {
            if (HttpConnectionUtil.result == "success") {
                Toast.makeText(PointAddActivity.this, "添加成功!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(PointAddActivity.this, "添加失败!", Toast.LENGTH_SHORT).show();
            }
            proDia.dismiss();
            point_name.setText("");
            paramMap.clear();
//            finish(); //如果finish(), 则返回前一个ctivity
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy");
        mMapView.onDestroy();
    }
}
